home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Gekkan Dennou Club 147
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan).7z
/
Gekkan Dennou Club - 2000.8 Vol. 147 (Japan) (Track 1).bin
/
tools
/
zmc3v078
/
zmc3v078.lzh
/
SRCSV078.LZH
/
READFILE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1999-07-31
|
5KB
|
220 lines
/* ======================================================
read file
input: filename (you can omit extentions)
pointer to timestamp variable
supplement extentions(in/out)
output: pointer to readed file
====================================================== */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "switch.h"
#include "config.h"
UBYTE *readFile(BYTE *infilename,BYTE *outfilename,const char *inext[],const char *outext);
UBYTE *readFile1(BYTE *infilename,int len,const char *inext[], int mode);
UBYTE *readFile2(BYTE *filename);
#include "etc.h"
#ifndef UNIXY_OS /* for setmode() */
#include <fcntl.h>
#include <io.h>
#ifdef X68000
#include <unistd.h>
#endif
#endif
#ifndef SEEK_SET
#define SEEK_SET (0)
#define SEEK_CUR (1)
#define SEEK_END (2)
#endif
/* ====================================================
read file
input: filename (you can omit extentions)
pointer to timestamp variable
supplement extentions(in/out)
output: pointer to readed file
effect: set source/destination files
==================================================== */
UBYTE *readFile(BYTE *infilename,BYTE *outfilename,const char *inext[],const char *outext)
{
UBYTE *zbuf;
BYTE *filename_ext;
int i,flg = 1,len;
if (infilename[0] == '-' && getSwitchVal('c') == RES_VAL && !outfilename[0]) {
fatal(ILLEGALSWITCH,"Error: destination file is not specified.\n");
}
len = (strlen(infilename) > strlen(outfilename))? strlen(infilename) : strlen(outfilename);
filename_ext = (char*)emalloc(sizeof(char) * (len + 16), "filename_ext");
strcpy(filename_ext,infilename); /* read source filename */
zbuf = readFile1(filename_ext, len, inext, 1);
/* set destination filename */
if (outfilename[0]) {
strcpy(filename_ext,outfilename);
}
#ifdef _0
if (!isatty(1)) { /* if used stdout */
strcpy(filename_ext,"-");
} else {
#endif
for (i = strlen(filename_ext); i > 0; i--) {
if (filename_ext[i] == '.') {
flg = 0;
break;
}
}
if (flg) {
i = strlen(filename_ext);
}
strcpy(&filename_ext[i],outext);
if (!outfilename[0]) {
#ifdef UNIXY_OS
BYTE pathdelimita = '/';
#else
BYTE pathdelimita = '\\';
#endif
int i,p = -1;
for (i = 0; i < (int)strlen(filename_ext);) {
if (filename_ext[i] == pathdelimita) {
p = i;
}
#ifdef UNIXY_OS
i++;
#else /* when not UNIX, only SJIS is supported (in iskanji). */
i += (iskanji(filename_ext[i]))? 2 : 1;
#endif
}
if (p >= 0) { /* cut directory name */
memmove(filename_ext, &filename_ext[p + 1], strlen(filename_ext) - p + 1);
}
}
#ifdef _0
}
#endif
setOutFile(filename_ext);
efree(filename_ext,"filename_ext");
return zbuf;
}
/* readfile; w/o outfile supplements */
UBYTE *readFile1(BYTE *filename_ext,int len, const char *inext[], int mode)
{
UBYTE *zbuf;
char *infilename;
infilename = (char*)emalloc(sizeof(char) * (len + 16), "infilename");
strcpy(infilename, filename_ext); /* read source filename */
#ifdef _0
if (!isatty(0)) { /* if used stdin */
outfilename = infilename;
strcpy(filename_ext,"-");
}
#endif
zbuf = readFile2(filename_ext);
if (!zbuf) {
while (*inext) {
strcpy(filename_ext, infilename);
strcat(filename_ext,*inext++);
zbuf = readFile2(filename_ext);
if (zbuf) {
break;
}
}
if (!zbuf) {
fatal(FILENOTFOUND,"%s\tError: file not found.\n",infilename);
return (UBYTE*)NULL;
}
}
if (mode) {
setInFile(filename_ext);
}
efree(infilename,"infile");
return zbuf;
}
/* =======================
read file (main)
======================= */
UBYTE *readFile2(BYTE *filename)
{
FILE *fp;
UBYTE *zbuf;
DWORD fl = 0,fl2;
if (!strcmp(filename,"-")) { /* use standard input */
int c;
fl2 = 16 * 1024;
#ifndef UNIXY_OS /* set stdin to binary mode (MS-DOS only) */
#if defined (X68000) && defined (XCLIB)
fmode(stdin, _IOBINARY); /* for X68000(XC) */
#elif defined (TOWNS)
_setmode(stdin, _BINARY); /* for TOWNS(High-C) */
#else
setmode(fileno(stdin),O_BINARY); /* for Win32/16, DOS, OS/2, X680x0(libc) */
#endif
#endif /* UNIXY_OS */
zbuf = (UBYTE*)emalloc(sizeof(UBYTE) * fl2, "zbuf_in_readfile");
while ((c = fgetc(stdin)) != EOF) {
zbuf[fl++] = c;
if (fl >= fl2) {
fl2 *= 2;
zbuf = (UBYTE*)erealloc(zbuf, sizeof(UBYTE) * fl2, "zbuf_in_readfile");
}
}
fl--; /* delete EOF */
zbuf = (UBYTE*)erealloc(zbuf, sizeof(UBYTE) * (fl2 + 4 + 2), "zbuf_in_readfile");
} else {
fp = fopen(filename,"rb");
if (fp==(FILE *)NULL) {
return (UBYTE*)NULL;
}
/*fl = filelength(fileno(fp));*/
fseek(fp,0,SEEK_END);
fl = ftell(fp);
rewind(fp);
zbuf = (UBYTE*)emalloc(fl + 4 + 2, "zbuf_in_readfile_2");
fl2 = fread(zbuf,sizeof(UBYTE),fl,fp);
if (fl2 < fl) {
fatal(READERR,"%s\tError: read error.\n",filename);
}
fclose(fp);
}
zbuf[fl] = 0x0d;
zbuf[fl+1] = 0x0a;
zbuf[fl+2] = zbuf[fl+3] = zbuf[fl + 4] = zbuf[fl + 5] = 0xFF; /* gurdian */
return zbuf;
}